package com.jinxin.platform.cdacsrecord.core.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.DeviceParam;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.GateLockInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.JurenDoorDeviceInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.TreeCommunity;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.CommonCodeEntity;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.ResultEntity;
import com.jinxin.platform.cdacsrecord.core.pojo.vo.DataResult2;
import com.jinxin.platform.cdacsrecord.core.service.RestTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-07-29
 * @Author: Lingnan
 **/
@RestController
@RequestMapping("/doorDevice")
@Slf4j
public class DoorDeviceController {
    @Autowired
    private RestTemplateService restTemplateService;

    @RequestMapping("/list")
    public Map<String,Object> list(Integer limit, DeviceParam param,String companyId){
        if(!StringUtils.isEmpty(limit)){
            param.setRows(limit);
        }
        Map<String,Object> resultMap = new HashMap<>();
        ResultEntity resultEntity = restTemplateService.listDoorDevices(param,companyId);
        List<JurenDoorDeviceInfo> list = new ArrayList<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        long count = 0;
        if(resultEntity != null && "000".equals(resultEntity.getResultCode())){
            DataResult2 dataResult = (DataResult2)resultEntity.getResultContent();
            JSONArray jsonArray = dataResult.getRows();
            if(jsonArray != null){
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    JurenDoorDeviceInfo info = JSON.parseObject(JSON.toJSONString(jsonObject), JurenDoorDeviceInfo.class);
                    list.add(info);
                }
                count = dataResult.getRecords();
            }
        }
        resultMap.put("count",count);
        resultMap.put("msg","");
        resultMap.put("data", list);
        return resultMap;
    }

    @RequestMapping("/listBuildingTree")
    public Map<String,Object> listBuildingTree(String companyId){
        Map<String,Object> resultMap = new HashMap<>();
        ResultEntity resultEntity = restTemplateService.listBuildingAreaTree(companyId);
        if(resultEntity != null){
            if(!"000".equals(resultEntity.getResultCode())){
                resultMap.put("communityList", null);
                resultMap.put("buildingMap", null);
                return resultMap;
            }
            JSONArray dataResult = (JSONArray)resultEntity.getResultContent();
            List<TreeCommunity> communityList = new ArrayList<>(); // 小区列表
            Map<String,List<TreeCommunity>> buildingMap = new LinkedHashMap<>();
            for (int i = 0; i < dataResult.size(); i++) {
                JSONObject jsonObject = dataResult.getJSONObject(i);
                TreeCommunity info = JSON.parseObject(JSON.toJSONString(jsonObject), TreeCommunity.class);
                if(info != null){
                    communityList.add(info);
                    JSONObject jo = info.getAdditionalParameters();
                    List<TreeCommunity> childrenList = JSON.parseArray(jo.get("children").toString(), TreeCommunity.class);
                    buildingMap.put(info.getId()+"", childrenList);
                }
            }
            resultMap.put("communityList", communityList);
            resultMap.put("buildingMap", buildingMap);
        }
        return resultMap;
    }

    @PostMapping("/gateLock/init")
    public ResultEntity initGateLock(@Valid GateLockInfo gateLockInfo, BindingResult bindingResult,String companyId){
        if(bindingResult.hasErrors()){
            String msg = Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage();
            return new ResultEntity(CommonCodeEntity.FAIL,msg, null);
        }
        return restTemplateService.initGateLock(gateLockInfo,companyId);
    }
}
